package uilayer;

import java.util.InputMismatchException;
import java.util.Scanner;
import modellayer.Person;
import controllayer.*;

/**
 * Textbased UI which allows the user to input information about persons.
 * 
 *@author Benjamin Ørskov
 *@author Lasse Lambertsen
 *@author Ole Kirk
 *@author Søren Søgaard
 */
public class PersonUI extends MenuInputs {

	// instance variables - object by the type iCtrl
	/** The person ctrl. */
	private PersonController pCtrl;

	/** The instance. */
	private static PersonUI instance;

	/**
	 * Constructor for PersonUI.
	 */
	private PersonUI() {
		// Initialize iCtrl
		pCtrl = new PersonController();
	}

	/**
	 * Gets the single instance of PersonUI.
	 *
	 * @return single instance of PersonUI
	 */
	public static PersonUI getInstance() {
		if (instance == null) {
			instance = new PersonUI();
		}
		return instance;
	}

	/**
	 * Person menu.
	 */
	public void personMenu() {
		boolean exit = false;
		try {
			while (!exit) { // that is: while exit is false
				int choice = writePersonMenu();
				if (choice == 1) { // Create person
					personCreateMenu();
				} else if (choice == 2) { // find person
					personFindMenu();

				} else if (choice == 3) { // update person
					personUpdateMenu();

				} else if (choice == 4) {
					personRemoveMenu();
				}

				else {
					exit = true;
				}// end else
			}// end while
		}// end try
		catch (InputMismatchException e) {
			// do something clever with the exception
			System.out.println("Du skal indtaste et tal!");
			personMenu();
		}
	}

	/**
	 * Display person menu.
	 *
	 * @return the int
	 */
	private int writePersonMenu() { // makes an object keyboard to read input
		// from the screen
		Scanner keyboard = new Scanner(System.in);
		System.out.println("*** Person menu ***");
		System.out.println(" (1) Tilføj person");
		System.out.println(" (2) Find person");
		System.out.println(" (3) Rediger person");
		System.out.println(" (4) Slet person");
		System.out.println(" (5) Hovedmenu");
		System.out.print("\n Vælg: ");

		int choice = keyboard.nextInt();
		return choice;
	}

	/**
	 * Person create menu.
	 */
	private void personCreateMenu() {
		boolean exit = false;
		try {
			while (!exit) { // that is: while exit is false
				int choice = writePersonCreateMenu();
				if (choice == 1) { // Create business person
					String cvr = inputCvr();
					try {
						pCtrl.findBusinessCustomer(cvr);
					} catch (NullPointerException npe) {
						pCtrl.createBusinessCustomer(inputName(),
								inputAddress(), inputCity(), inputEmail(),
								inputPhone(), cvr, inputDiscount());
					}
				} else if (choice == 2) { // create privateperson
					String phone = inputPhone();
					try {
						pCtrl.findCustomer(phone);
					} catch (NullPointerException npe) {
						pCtrl.createCustomer(inputName(), inputAddress(),
								inputCity(), inputEmail(), phone);
					}
				} else if (choice == 3) { // create salesman
					try {
						System.out.println(pCtrl.createSalesman(inputName(),
								inputAddress(), inputCity(), inputEmail(),
								inputPhone(), inputPaycheck()));
					} catch (NullPointerException npe) {
					}
				} else {
					exit = true;
				}// end else
			}// end while
		}// end try
		catch (InputMismatchException e) {
			// do something clever with the exception
			System.out.println("Du skal indtaste et tal!");
			personMenu();
		}
	}

	/**
	 * Write person create menu.
	 *
	 * @return the int
	 */
	private int writePersonCreateMenu() { // makes an object keyboard to read
		// input from the screen
		Scanner keyboard = new Scanner(System.in);
		System.out.println("*** Tilføj person menu ***");
		System.out.println(" (1) Tilføj erhvervskunde");
		System.out.println(" (2) Tilføj privatkunde");
		System.out.println(" (3) Tilføj sælger");
		System.out.println(" (4) Tilbage");
		System.out.print("\n Vælg: ");

		int choice = keyboard.nextInt();
		return choice;
	}

	/**
	 * Person find menu.
	 */
	private void personFindMenu() {
		boolean exit = false;
		try {
			while (!exit) { // that is: while exit is false
				int choice = writePersonFindMenu();
				if (choice == 1) { // find business person
					try {
						Person tempP = pCtrl.findBusinessCustomer(inputCvr());
						System.out.println("\nResultat:\n\n" + tempP);
					} catch (NullPointerException npe) {
						System.out.println(npe.getMessage());
					}
				} else if (choice == 2) { // find private person
					try {
						Person tempP = pCtrl.findCustomer(inputPhone());
						System.out.println("\nResultat:\n\n" + tempP);
					} catch (NullPointerException npe) {
						System.out.println(npe.getMessage());
					}
				} else if (choice == 3) { // find salesman
					try {
						Person tempP = pCtrl.findSalesman(inputID());
						System.out.println("\nResultat:\n\n" + tempP);
					} catch (NullPointerException npe) {
						System.out.println(npe.getMessage());
					}
				} else {
					exit = true;
				}// end else
			}// end while
		}// end try
		catch (InputMismatchException e) {
			// do something clever with the exception
			System.out.println("Du skal indtaste et tal!");
			personMenu();
		}
	}

	/**
	 * Write person find menu.
	 *
	 * @return the int
	 */
	private int writePersonFindMenu() { // makes an object keyboard to read
		// input from the screen
		Scanner keyboard = new Scanner(System.in);
		System.out.println("*** Find person menu ***");
		System.out.println(" (1) Find erhvervskunde");
		System.out.println(" (2) Find privatkunde");
		System.out.println(" (3) Find sælger");
		System.out.println(" (4) Tilbage");
		System.out.print("\n Vælg: ");

		int choice = keyboard.nextInt();
		return choice;
	}

	/**
	 * Person update menu.
	 */
	private void personUpdateMenu() {
		boolean exit = false;
		try {
			while (!exit) { // that is: while exit is false
				int choice = writePersonUpdateMenu();
				if (choice == 1) { // Update business person
					try {
						Person tempP = pCtrl.findBusinessCustomer(inputCvr());
						pCtrl.updateBusinessCustomer(tempP, inputName(),
								inputAddress(), inputCity(), inputEmail(),
								inputPhone(), inputCvr(), inputDiscount());
					} catch (UpdateIncompleteException uie) {
						System.out.println(uie.getMessage());
					}
				} else if (choice == 2) { // Update private person
					try {
						Person tempP = pCtrl.findCustomer(inputPhone());
						pCtrl.updateCustomer(tempP, inputName(),
								inputAddress(), inputCity(), inputEmail(),
								inputPhone());
						;
					} catch (UpdateIncompleteException uie) {
						System.out.println(uie.getMessage());
					}
				} else if (choice == 3) { // Update salesman
					try {
						Person tempP = pCtrl.findSalesman(inputID());
						pCtrl.updateSalesman(tempP, inputName(),
								inputAddress(), inputCity(), inputEmail(),
								inputPhone(), inputPaycheck(), inputBonus());
						;
					} catch (UpdateIncompleteException uie) {
						System.out.println(uie.getMessage());
					}
				} else {
					exit = true;
				}// end else
			}// end while
		}// end try
		catch (InputMismatchException e) {
			// do something clever with the exception
			System.out.println("Du skal indtaste et tal!");
			personMenu();
		}
	}

	/**
	 * Write person update menu.
	 *
	 * @return the int
	 */
	private int writePersonUpdateMenu() { // makes an object keyboard to read
		// input from the screen
		Scanner keyboard = new Scanner(System.in);
		System.out.println("*** Rediger person menu ***");
		System.out.println(" (1) Rediger erhvervskunde");
		System.out.println(" (2) Rediger privatkunde");
		System.out.println(" (3) Rediger sælger");
		System.out.println(" (4) Tilbage");
		System.out.print("\n Vælg: ");

		int choice = keyboard.nextInt();
		return choice;
	}

	/**
	 * Person remove menu.
	 */
	private void personRemoveMenu() {
		boolean exit = false;
		try {
			while (!exit) { // that is: while exit is false
				int choice = writePersonUpdateMenu();
				if (choice == 1) { // Update business person
					try {
						Person tempP = pCtrl.findBusinessCustomer(inputCvr());
						pCtrl.removePerson(tempP);
					} catch (NullPointerException npe) {
						System.out.println(npe.getMessage());
					}
				} else if (choice == 2) { // Update private person
					try {
						Person tempP = pCtrl.findCustomer(inputPhone());
						pCtrl.removePerson(tempP);
					} catch (NullPointerException npe) {
						System.out.println(npe.getMessage());
					}
				} else if (choice == 3) { // Update salesman
					try {
						Person tempP = pCtrl.findSalesman(inputID());
						pCtrl.removePerson(tempP);
					} catch (NullPointerException npe) {
						System.out.println(npe.getMessage());
					}
				} else {
					exit = true;
				}// end else
			}// end while
		}// end try
		catch (InputMismatchException e) {
			// do something clever with the exception
			System.out.println("Du skal indtaste et tal!");
			personMenu();
		}
	}

	/**
	 * Write person remove menu.
	 *
	 * @return the int
	 */
	private int writePersonRemoveMenu() { // makes an object keyboard to read
		// input from the screen
		Scanner keyboard = new Scanner(System.in);
		System.out.println("*** Vare menu ***");
		System.out.println(" (1) Slet erhvervskunde");
		System.out.println(" (2) Slet privatkunde");
		System.out.println(" (3) Slet sælger");
		System.out.println(" (4) Tilbage");
		System.out.print("\n Vælg: ");

		int choice = keyboard.nextInt();
		return choice;
	}
}